package dao

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"time"
)

var DB *gorm.DB

/**
 * @Author leibidong
 * @Description 初始化数据库连接
 * @Date  2021/7/11 17:34
 **/
func InitDB() *gorm.DB {
	DB, err := gorm.Open("mysql", "root:root@/dongchedi?charset=utf8mb4&parseTime=True&loc=Local")
	if err != nil {
		fmt.Println(err)
	}
	DB.DB().SetMaxIdleConns(10)
	DB.DB().SetMaxOpenConns(20)
	DB.DB().SetConnMaxLifetime(time.Hour)
	return DB
}

/**
 * @Author leibidong
 * @Description 关闭数据库连接
 * @Date  2021/7/11 17:34
 **/
func Close() {
	DB.Close()
}
